<?php
namespace app\tripapi\model;
use think\Model;
use think\Db;
class TravelApply extends Model
{
    public function TravelApplySave($params,$uid)
    {
        $output = '';
        $time = time();
        $json = array();
        $json1 = array();
        $json2 = array();
        // 接收travel apply 数据表参数信息
        $params['traffic'] = iconv('UTF-8', 'gbk', $params['traffic']); // 交通工具
        $params['traveler'] = iconv('UTF-8', 'gbk', $params['traveler']); // 交通工具
        $params['department'] = iconv('UTF-8', 'gbk', $params['department']); // 交通工具
        $params['apply_user'] = iconv('UTF-8', 'gbk', $params['apply_user']); // 交通工具
        $params['charge_user'] = iconv('UTF-8', 'gbk', $params['charge_user']); // 交通工具
        $params['travel_place'] = iconv('UTF-8', 'gbk', $params['travel_place']); // 交通工具
        $params['reason'] = iconv('UTF-8', 'gbk', $params['reason']); // 交通工具
        $params['contacter'] = iconv('UTF-8', 'gbk', $params['contacter']); // 交通工具
        $params['target'] = iconv('UTF-8', 'gbk', $params['target']); // 交通工具
        $params['creator'] = iconv('UTF-8', 'gbk', $params['creator']); // 交通工具
        $params['financial_advance'] = $params['financial_advance']; // 交通工具
        $params['creator_id'] = $params['creator_id'];
        $params['create_time'] = $time;
        $traffic_self_list = $params['number2'];
        foreach ($traffic_self_list as $ky => $val) {
            $json[$ky]['traffic_self'] = $params['roundtrip'][$ky]; // 部门预定 TODO: 组装json
            $json[$ky]['peoplecount'] = $params['peoplecount'][$ky]; // 部门预定 TODO: 组装json
            $json[$ky]['arrivetotime'] = $params['arrivetotime'][$ky]; // 部门预定 TODO: 组装json
            $json[$ky]['flight'] = $params['flight'][$ky]; // 部门预定 TODO: 组装json
            $json[$ky]['context'] = $params['context'][$ky]; // 部门预定 TODO: 组装json
        }
        $traffic_business_receive_list = $params['number3'];
        foreach ($traffic_business_receive_list as $k => $v) {
            $json1[$k]['StartTime'] = $params['StartTime'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['onwer'] = $params['onwer'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['customer'] = $params['customer'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['detailreason'] = $params['detailreason'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['receive'] = $params['receive'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['businessreason'] = $params['businessreason'][$k]; // 业务招待 TODO: 组装json
            $params['business_money'] = array_sum($params['receive']); // 业务费预算小计
        }
        $params['traffic_self'] = json_encode($json); // 部门预定 TODO: 组装json
        $params['business_receive'] = json_encode($json1); //业务招待 TODO: 组装json
        $json2['0']['project_name'] = "交通费"; // 费用预算 --- 项目名
        $json2['0']['trafficdetail'] = $params['trafficdetail']; // 费用预算 --- 详细内容
        $json2['0']['traffictotal'] = $params['traffictotal']; // 费用预算 --- 决算金额
        $json2['0']['trafficreason'] = $params['trafficreason']; // 费用预算 --- 备注
        $json2['1']['project_name'] = "住宿费"; // 费用预算 --- 项目名
        $json2['1']['staydetail'] = $params['staydetail']; // 费用预算 --- 详细内容
        $json2['1']['staytotal'] = $params['staytotal']; // 费用预算 --- 决算金额
        $json2['1']['stayreason'] = $params['stayreason']; // 费用预算 --- 备注
        $json2['2']['project_name'] = "餐费"; // 费用预算 --- 项目名
        $json2['2']['foodetail'] = $params['foodetail']; // 费用预算 --- 详细内容
        $json2['2']['foototal'] = $params['foototal']; // 费用预算 --- 决算金额
        $json2['2']['fooreason'] = $params['fooreason']; // 费用预算 --- 备注
        $json2['3']['project_name'] = "招待费"; // 费用预算 --- 项目名
        $json2['3']['meetdetail'] = $params['meetdetail']; // 费用预算 --- 详细内容
        $json2['3']['meettotal'] = $params['meettotal']; // 费用预算 --- 决算金额
        $json2['3']['meetreason'] = $params['meetreason']; // 费用预算 --- 备注
        $json2['4']['project_name'] = "出差补贴"; // 费用预算 --- 项目名
        $json2['4']['traveldetail'] = $params['traveldetail']; // 费用预算 --- 详细内容
        $json2['4']['traveltotal'] = $params['traveltotal']; // 费用预算 --- 决算金额
        $json2['4']['travelreason'] = $params['travelreason']; // 费用预算 --- 备注
        $json2['5']['project_name'] = "其他费用"; // 费用预算 --- 项目名
        $json2['5']['otherdetail'] = $params['otherdetail']; // 费用预算 --- 详细内容
        $json2['5']['othertotal'] = $params['othertotal']; // 费用预算 --- 决算金额
        $json2['5']['otherreason'] = $params['otherreason']; // 费用预算 --- 备注
        $params['budget_item'] = json_encode($json2); // 费用预算 TODO: 组装json
        $params['budget_money'] = $params['traffictotal'] + $params['staytotal'] + $params['foototal'] + $params['meettotal'] + $params['traveltotal'] + $params['othertotal']; // 费用预算合计
        //$travel_apply['cost_bear'] = $_POST['cost_bear']; // 费用承担
        $params['depart_opinion'] = iconv('UTF-8', 'gbk', $params['depart_opinion']); // 部门意见
        $params['politic_opinion'] = iconv('UTF-8', 'gbk', $params['politic_opinion']); // 行政意见
        $params['finance_opinion'] = iconv('UTF-8', 'gbk', $params['finance_opinion']); // 财务意见
        $params['finance_total'] = $params['finance_total']; // 财务建议支出金额
        $params['manager_opinion'] = iconv('UTF-8', 'gbk', $params['manager_opinion']); // 总经理意见
        $params['remark'] = iconv('UTF-8', 'gbk', $params['remark']); // 申请单备注
        $approve = Db::name('new_user')->where('uid', '=', $params['creator_id'])->select();
        $xzuser = Db::name('approval_proces')->where('department_name', 'like', "%".$approve['0']['department_name'])->select();
        $app = Db::name('new_user')->where('user_name', '=', $xzuser['0']['step3'])->select();
        unset($params['number']);unset($params['number1']);unset($params['number2']);unset($params['number3']);unset($params['Time']);unset($params['trip_plan']);unset($params['live_place']);unset($params['customer_name']);unset($params['item_id']);unset($params['roundtrip']);unset($params['peoplecount']);unset($params['arrivetotime']);unset($params['flight']);unset($params['context']);unset($params['StartTime']);unset($params['onwer']);unset($params['customer']);unset($params['detailreason']);unset($params['receive']);unset($params['businessreason']);unset($params['alltotal']);unset($params['trafficdetail']);unset($params['traffictotal']);unset($params['trafficreason']);unset($params['staydetail']);unset($params['staytotal']);unset($params['stayreason']);unset($params['foodetail']);unset($params['foototal']);unset($params['fooreason']);unset($params['meetdetail']);unset($params['meettotal']);unset($params['meetreason']);unset($params['traveldetail']);unset($params['traveltotal']);unset($params['travelreason']);unset($params['otherdetail']);unset($params['othertotal']);unset($params['otherreason']);unset($params['cost_bearing']);
        if($approve['0']['user_name'] == $xzuser['0']['step1']){ //以第一审批人为准
            $params['to_user_step'] = $xzuser['0']['step1']; // 部门领导审批
            $params['to_status_step'] = '1'; // 0 待审批 1 审批通过  2 驳回  3 关闭
            $params['to_uid_step'] = $this->GetApproveListByName($params['to_user_step']);
            $params['to_user_step1'] = $xzuser['0']['step3']; // 行政查阅人姓名
            $params['to_status_step1'] = '0'; // 0 待审批 1 审批通过  2 驳回  3 关闭.
            $params['to_uid_step1'] = $app['0']['uid']; // 行政查阅人id
            $id = Db::name('travel_apply')->insert($params, false, true);
            $output = CustomergetUserIdByUid($this->GetApproveListByName($params['to_user_step1'])).",".'您有一条待审批的出差单明细'.","."erp5/public/tripapi/travel/index_hr?nids=".$id;
            $id = Db::name('travel_apply')->insert($params, false, true);
        }else{
            $params['to_user_step'] = $xzuser['0']['step1'];
            $params['to_status_step'] = '0'; // 0 待审批 1 审批通过  2 驳回  3 关闭
            $params['to_uid_step'] = $this->GetApproveListByName($params['to_user_step']);
            $id = Db::name('travel_apply')->insert($params, false, true);
            $output = CustomergetUserIdByUid($this->GetApproveListByName($params['to_user_step'])).",".'您有一条待审批的出差单明细'.","."erp5/public/tripapi/travel/index_pt?nids=".$id;
        }
        // 保存travel apply 数据表
        return array('id' => $id, 'params' => $output);
    }

    /*
     *  获取列表
     */
    public function GetOtherList($data,$uid, $nid){
        if($nid == "null "){
            $res = Db::name('travel_apply')->where(function($query) use ($uid) {
                $query->where('to_uid_step','=',$uid);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step1','=',$uid);
            })->select();
            $count = count($res);
            $current_page = $data['page'];
            $startSize = "10";
            $lastSize = $startSize*($current_page - 1);
            $result = Db::name('travel_apply')->where(function($query) use ($uid) {
                $query->where('to_uid_step','=',$uid);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step1','=',$uid);
            })->order('id', 'desc')->limit($lastSize,$startSize)->select();
        }else{
            $res = Db::name('travel_apply')->where(function($query) use ($nid, $uid) {
                $query->where('to_uid_step','=',$uid)->where('id','=',$nid);
            })->whereor(function($query) use ($nid, $uid){
                $query->where('to_uid_step1','=',$uid)->where('id','=',$nid);
            })->select();
            $count = count($res);
            $current_page = $data['page'];
            $startSize = "10";
            $lastSize = $startSize*($current_page - 1);
            $result = Db::name('travel_apply')->where(function($query) use ($nid, $uid) {
                $query->where('to_uid_step','=',$uid)->where('id','=',$nid);
            })->whereor(function($query) use ($nid, $uid){
                $query->where('to_uid_step1','=',$uid)->where('id','=',$nid);
            })->order('id', 'desc')->limit($lastSize,$startSize)->select();
        }
        foreach ($result as $key => $value) {
            $result[$key]['traveler'] = iconv('GBK', 'UTF-8', $value['traveler']);
            $result[$key]['department'] = iconv('GBK', 'UTF-8', $value['department']);
            $result[$key]['apply_user'] = iconv('GBK', 'UTF-8', $value['apply_user']);
            $result[$key]['charge_user'] = iconv('GBK', 'UTF-8', $value['charge_user']);
            $result[$key]['reason'] = iconv('GBK', 'UTF-8', $value['reason']);
            $result[$key]['travel_place'] = iconv('GBK', 'UTF-8', $value['travel_place']);
            $result[$key]['reason'] = iconv('GBK', 'UTF-8', $value['reason']);
            $result[$key]['contacter'] = iconv('GBK', 'UTF-8', $value['contacter']);
            $result[$key]['target'] = iconv('GBK', 'UTF-8', $value['target']);
            $result[$key]['traffic'] = iconv('GBK', 'UTF-8', $value['traffic']);

            $traffic_self = json_decode($value['traffic_self']);
            foreach ($traffic_self as $ky => $val) {
                $result[$key][$ky]['trafficself'] = $val->traffic_self;
                $result[$key][$ky]['peoplecount'] = $val->peoplecount;
                $result[$key][$ky]['arrivetotime'] = $val->arrivetotime;
                $result[$key][$ky]['flight'] = $val->flight;
                $result[$key][$ky]['context'] = $val->context;
            }

            $business_receive = json_decode($value['business_receive']);
            foreach ($business_receive as $k => $v) {
                $result[$key][$k]['StartTime'] = $v->StartTime; // 业务招待 TODO: 组装json
                $result[$key][$k]['onwer'] = $v->onwer; // 业务招待 TODO: 组装json
                $result[$key][$k]['customer'] = $v->customer; // 业务招待 TODO: 组装json
                $result[$key][$k]['detailreason'] = $v->detailreason; // 业务招待 TODO: 组装json
                $result[$key][$k]['receive'] = $v->receive; // 业务招待 TODO: 组装json
                $result[$key][$k]['businessreason'] = $v->businessreason; // 业务招待 TODO: 组装json
            }

            $budget_item = json_decode($value['budget_item']);
            $result[$key]['project_name'] = "交通费"; // 费用预算 --- 项目名
            $result[$key]['trafficdetail'] = $budget_item['0']->trafficdetail; // 费用预算 --- 详细内容
            $result[$key]['traffictotal'] = $budget_item['0']->traffictotal; // 费用预算 --- 决算金额
            $result[$key]['trafficreason'] = $budget_item['0']->trafficreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "住宿费"; // 费用预算 --- 项目名
            $result[$key]['staydetail'] = $budget_item['1']->staydetail; // 费用预算 --- 详细内容
            $result[$key]['staytotal'] = $budget_item['1']->staytotal; // 费用预算 --- 决算金额
            $result[$key]['stayreason'] = $budget_item['1']->stayreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "餐费"; // 费用预算 --- 项目名
            $result[$key]['foodetail'] = $budget_item['2']->foodetail; // 费用预算 --- 详细内容
            $result[$key]['foototal'] = $budget_item['2']->foototal; // 费用预算 --- 决算金额
            $result[$key]['fooreason'] = $budget_item['2']->fooreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "招待费"; // 费用预算 --- 项目名
            $result[$key]['meetdetail'] = $budget_item['3']->meetdetail; // 费用预算 --- 详细内容
            $result[$key]['meettotal'] = $budget_item['3']->meettotal; // 费用预算 --- 决算金额
            $result[$key]['meetreason'] = $budget_item['3']->meetreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "出差补贴"; // 费用预算 --- 项目名
            $result[$key]['traveldetail'] = $budget_item['4']->traveldetail; // 费用预算 --- 详细内容
            $result[$key]['traveltotal'] = $budget_item['4']->traveltotal; // 费用预算 --- 决算金额
            $result[$key]['travelreason'] = $budget_item['4']->travelreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "其他费用"; // 费用预算 --- 项目名
            $result[$key]['otherdetail'] = $budget_item['5']->otherdetail; // 费用预算 --- 详细内容
            $result[$key]['othertotal'] = $budget_item['5']->othertotal; // 费用预算 --- 决算金额
            $result[$key]['otherreason'] = $budget_item['5']->otherreason; // 费用预算 --- 备注
            $result[$key]['depart_opinion'] = iconv('GBK', 'UTF-8', $value['depart_opinion']);
            $result[$key]['politic_opinion'] = iconv('GBK', 'UTF-8', $value['politic_opinion']);
            $result[$key]['finance_opinion'] = iconv('GBK', 'UTF-8', $value['finance_opinion']);
            $result[$key]['manager_opinion'] = iconv('GBK', 'UTF-8', $value['manager_opinion']);
            $result[$key]['remark'] = iconv('GBK', 'UTF-8', $value['remark']);
            $result[$key]['creator'] = iconv('GBK', 'UTF-8', $value['creator']);
            $result[$key]['to_user_step'] = iconv('GBK', 'UTF-8', $value['to_user_step']);
            $result[$key]['to_user_step1'] = iconv('GBK', 'UTF-8', $value['to_user_step1']);
            $result[$key]['create_time'] = date('Y-m-d', $value['create_time']);
            if($uid == $value['creator_id']){
                $result[$key]['isshow'] = "false";
            }else{
                $result[$key]['isshow'] = "true";
            }
            if($value['to_status_step'] == '0'){
                $result[$key]['status'] = "部门经理审批中";
            }elseif($value['to_status_step1'] == '2'){
                $result[$key]['status'] = "总经理审批中";
            }elseif($value['to_status_step1'] == '5'){
                $result[$key]['status'] = "人事存档";
            }elseif($value['to_status_step1'] == '3'){
                $result[$key]['status'] = "行政审批中";
            }if($value['to_status_step1'] == '4'){
                $result[$key]['status'] = "财务审批中";
            }
            $res = Db::name('travel_apply_customer')->where([
                'apply_id'=>['=',$value['id']],
                'travel_status'=>['=','publish']
            ])->select();
            foreach($res as $k => $val){
                $result[$key][$k]['trip_plan'] = iconv('GBK','UTF-8', $val['trip_plan']);
                $result[$key][$k]['live_place'] = iconv('GBK','UTF-8', $val['live_place']);
                $result[$key][$k]['customer_name'] = iconv('GBK','UTF-8', $val['customer_name']);
                $result[$key][$k]['creator'] = iconv('GBK','UTF-8', $val['creator']);
            }
        }
        return $result;
    }

    // 获取apply id
    public function GetApplyId($id,$uid, $data, $nid)
    {
        if($nid == "null "){
            $res = Db::name('travel_apply')->where(function($query) use ($id) {
                $query->where('apply_num','=',$id);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step1','=',$uid);
            })->select();
            $count = count($res);
            $current_page = $data['page'];
            $startSize = "10";
            $lastSize = $startSize*($current_page - 1);
            $result = Db::name('travel_apply')->where(function($query) use ($id) {
                $query->where('apply_num','=',$id);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step1','=',$uid);
            })->order('id', 'desc')->limit($lastSize,$startSize)->select();
        }else{
            $res = Db::name('travel_apply')->where(function($query) use ($nid, $id) {
                $query->where('apply_num','=',$id)->where('id','=',$nid);
            })->whereor(function($query) use ($nid, $uid){
                $query->where('to_uid_step1','=',$uid)->where('id','=',$nid);
            })->select();
            $count = count($res);
            $current_page = $data['page'];
            $startSize = "10";
            $lastSize = $startSize*($current_page - 1);
            $result = Db::name('travel_apply')->where(function($query) use ($nid, $id) {
                $query->where('apply_num','=',$id)->where('id','=',$nid);
            })->whereor(function($query) use ($nid, $uid){
                $query->where('to_uid_step1','=',$uid)->where('id','=',$nid);
            })->order('id', 'desc')->limit($lastSize,$startSize)->select();
        }
        foreach ($result as $key => $value) {
            $result[$key]['traveler'] = iconv('GBK', 'UTF-8', $value['traveler']);
            $result[$key]['department'] = iconv('GBK', 'UTF-8', $value['department']);
            $result[$key]['apply_user'] = iconv('GBK', 'UTF-8', $value['apply_user']);
            $result[$key]['charge_user'] = iconv('GBK', 'UTF-8', $value['charge_user']);
            $result[$key]['reason'] = iconv('GBK', 'UTF-8', $value['reason']);
            $result[$key]['travel_place'] = iconv('GBK', 'UTF-8', $value['travel_place']);
            $result[$key]['reason'] = iconv('GBK', 'UTF-8', $value['reason']);
            $result[$key]['contacter'] = iconv('GBK', 'UTF-8', $value['contacter']);
            $result[$key]['target'] = iconv('GBK', 'UTF-8', $value['target']);
            $result[$key]['traffic'] = iconv('GBK', 'UTF-8', $value['traffic']);

            $traffic_self = json_decode($value['traffic_self']);
            foreach ($traffic_self as $ky => $val) {
                $result[$key][$ky]['trafficself'] = $val->traffic_self;
                $result[$key][$ky]['peoplecount'] = $val->peoplecount;
                $result[$key][$ky]['arrivetotime'] = $val->arrivetotime;
                $result[$key][$ky]['flight'] = $val->flight;
                $result[$key][$ky]['context'] = $val->context;
            }

            $business_receive = json_decode($value['business_receive']);
            foreach ($business_receive as $k => $v) {
                $result[$key][$k]['StartTime'] = $v->StartTime; // 业务招待 TODO: 组装json
                $result[$key][$k]['onwer'] = $v->onwer; // 业务招待 TODO: 组装json
                $result[$key][$k]['customer'] = $v->customer; // 业务招待 TODO: 组装json
                $result[$key][$k]['detailreason'] = $v->detailreason; // 业务招待 TODO: 组装json
                $result[$key][$k]['receive'] = $v->receive; // 业务招待 TODO: 组装json
                $result[$key][$k]['businessreason'] = $v->businessreason; // 业务招待 TODO: 组装json
            }

            $budget_item = json_decode($value['budget_item']);
            $result[$key]['project_name'] = "交通费"; // 费用预算 --- 项目名
            $result[$key]['trafficdetail'] = $budget_item['0']->trafficdetail; // 费用预算 --- 详细内容
            $result[$key]['traffictotal'] = $budget_item['0']->traffictotal; // 费用预算 --- 决算金额
            $result[$key]['trafficreason'] = $budget_item['0']->trafficreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "住宿费"; // 费用预算 --- 项目名
            $result[$key]['staydetail'] = $budget_item['1']->staydetail; // 费用预算 --- 详细内容
            $result[$key]['staytotal'] = $budget_item['1']->staytotal; // 费用预算 --- 决算金额
            $result[$key]['stayreason'] = $budget_item['1']->stayreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "餐费"; // 费用预算 --- 项目名
            $result[$key]['foodetail'] = $budget_item['2']->foodetail; // 费用预算 --- 详细内容
            $result[$key]['foototal'] = $budget_item['2']->foototal; // 费用预算 --- 决算金额
            $result[$key]['fooreason'] = $budget_item['2']->fooreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "招待费"; // 费用预算 --- 项目名
            $result[$key]['meetdetail'] = $budget_item['3']->meetdetail; // 费用预算 --- 详细内容
            $result[$key]['meettotal'] = $budget_item['3']->meettotal; // 费用预算 --- 决算金额
            $result[$key]['meetreason'] = $budget_item['3']->meetreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "出差补贴"; // 费用预算 --- 项目名
            $result[$key]['traveldetail'] = $budget_item['4']->traveldetail; // 费用预算 --- 详细内容
            $result[$key]['traveltotal'] = $budget_item['4']->traveltotal; // 费用预算 --- 决算金额
            $result[$key]['travelreason'] = $budget_item['4']->travelreason; // 费用预算 --- 备注
            $result[$key]['project_name'] = "其他费用"; // 费用预算 --- 项目名
            $result[$key]['otherdetail'] = $budget_item['5']->otherdetail; // 费用预算 --- 详细内容
            $result[$key]['othertotal'] = $budget_item['5']->othertotal; // 费用预算 --- 决算金额
            $result[$key]['otherreason'] = $budget_item['5']->otherreason; // 费用预算 --- 备注
            $result[$key]['depart_opinion'] = iconv('GBK', 'UTF-8', $value['depart_opinion']);
            $result[$key]['politic_opinion'] = iconv('GBK', 'UTF-8', $value['politic_opinion']);
            $result[$key]['finance_opinion'] = iconv('GBK', 'UTF-8', $value['finance_opinion']);
            $result[$key]['manager_opinion'] = iconv('GBK', 'UTF-8', $value['manager_opinion']);
            $result[$key]['remark'] = iconv('GBK', 'UTF-8', $value['remark']);
            $result[$key]['creator'] = iconv('GBK', 'UTF-8', $value['creator']);
            $result[$key]['to_user_step'] = iconv('GBK', 'UTF-8', $value['to_user_step']);
            $result[$key]['to_user_step1'] = iconv('GBK', 'UTF-8', $value['to_user_step1']);
            $result[$key]['create_time'] = date('Y-m-d', $value['create_time']);
            if($uid == $value['creator_id']){
                $result[$key]['isshow'] = "false";
            }else{
                $result[$key]['isshow'] = "true";
            }
            if($value['to_status_step'] == '0'){
                $result[$key]['status'] = "部门经理审批中";
            }elseif($value['to_status_step1'] == '2'){
                $result[$key]['status'] = "总经理审批中";
            }elseif($value['to_status_step1'] == '5'){
                $result[$key]['status'] = "人事存档";
            }elseif($value['to_status_step1'] == '3'){
                $result[$key]['status'] = "行政审批中";
            }if($value['to_status_step1'] == '4'){
                $result[$key]['status'] = "财务审批中";
            }
            $result[$key]['count'] = $count;
        }
        return $result;
    }

    /*
     * 获取根据id明细
     */
    public function GetItemById($id)
    {
        $result = Db::name('travel_apply')->where('id', '=', $id)->select();
        foreach ($result as $key => $value) {
            $result[$key]['traveler'] = iconv('GBK', 'UTF-8', $value['traveler']);
            $result[$key]['department'] = iconv('GBK', 'UTF-8', $value['department']);
            $result[$key]['apply_user'] = iconv('GBK', 'UTF-8', $value['apply_user']);
            $result[$key]['charge_user'] = iconv('GBK', 'UTF-8', $value['charge_user']);
            $result[$key]['reason'] = iconv('GBK', 'UTF-8', $value['reason']);
            $result[$key]['travel_place'] = iconv('GBK', 'UTF-8', $value['travel_place']);
            $result[$key]['reason'] = iconv('GBK', 'UTF-8', $value['reason']);
            $result[$key]['contacter'] = iconv('GBK', 'UTF-8', $value['contacter']);
            $result[$key]['target'] = iconv('GBK', 'UTF-8', $value['target']);
            $result[$key]['traffic'] = iconv('GBK', 'UTF-8', $value['traffic']);
            $result[$key]['depart_opinion'] = iconv('GBK', 'UTF-8', $value['depart_opinion']);
            $result[$key]['politic_opinion'] = iconv('GBK', 'UTF-8', $value['politic_opinion']);
            $result[$key]['finance_opinion'] = iconv('GBK', 'UTF-8', $value['finance_opinion']);
            $result[$key]['manager_opinion'] = iconv('GBK', 'UTF-8', $value['manager_opinion']);
            $result[$key]['remark'] = iconv('GBK', 'UTF-8', $value['remark']);
            $result[$key]['creator'] = iconv('GBK', 'UTF-8', $value['creator']);
            $result[$key]['create_time'] = date('Y-m-d', $value['create_time']);
        }
        return $result;
    }

    public function ManageGetItemById($id)
    {
        $result = Db::name('travel_apply')->where('id','=', $id)->select();
        $count = count($result);
        foreach ($result as $key => $value) {
            $result[$key]['traveler'] = iconv('GBK', 'UTF-8', $value['traveler']);
            $result[$key]['department'] = iconv('GBK', 'UTF-8', $value['department']);
            $result[$key]['apply_user'] = iconv('GBK', 'UTF-8', $value['apply_user']);
            $result[$key]['charge_user'] = iconv('GBK', 'UTF-8', $value['charge_user']);
            $result[$key]['reason'] = iconv('GBK', 'UTF-8', $value['reason']);
            $result[$key]['travel_place'] = iconv('GBK', 'UTF-8', $value['travel_place']);
            $result[$key]['reason'] = iconv('GBK', 'UTF-8', $value['reason']);
            $result[$key]['contacter'] = iconv('GBK', 'UTF-8', $value['contacter']);
            $result[$key]['target'] = iconv('GBK', 'UTF-8', $value['target']);
            $result[$key]['traffic'] = iconv('GBK', 'UTF-8', $value['traffic']);
            $result[$key]['depart_opinion'] = iconv('GBK', 'UTF-8', $value['depart_opinion']);
            $result[$key]['politic_opinion'] = iconv('GBK', 'UTF-8', $value['politic_opinion']);
            $result[$key]['finance_opinion'] = iconv('GBK', 'UTF-8', $value['finance_opinion']);
            $result[$key]['manager_opinion'] = iconv('GBK', 'UTF-8', $value['manager_opinion']);
            $result[$key]['remark'] = iconv('GBK', 'UTF-8', $value['remark']);
            $result[$key]['creator'] = iconv('GBK', 'UTF-8', $value['creator']);
            $result[$key]['create_time'] = date('Y-m-d', $value['create_time']);
            $result[$key]['count'] = $count;
        }
        return $result;
    }

    public function GetApplyItemList($data){
        $result = Db::name('travel_apply')->select();
        $count = count($result);
        foreach($result as $key => $value){
            $result[$key]['count'] =  $count;
            $result[$key]['creator'] = iconv('GBK', 'UTF-8', $value['creator']);
        }
        return $result;
    }

    /*
     * 删除
     */
    public function ApplyDelete($id)
    {
        $result = Db::name('travel_apply')->where('id', '=', $id)->delete();
        return $result;
    }

    /*
     * 更新
     */
    public function ApplyUpdate($id, $params)
    {
        $time = time();
        $json = array();
        $json1 = array();
        $json2 = array();
        // 接收travel apply 数据表参数信息
        $params['traffic'] = iconv('UTF-8', 'gbk', $params['traffic']); // 交通工具
        $params['traveler'] = iconv('UTF-8', 'gbk', $params['traveler']); // 交通工具
        $params['department'] = iconv('UTF-8', 'gbk', $params['department']); // 交通工具
        $params['apply_user'] = iconv('UTF-8', 'gbk', $params['apply_user']); // 交通工具
        $params['charge_user'] = iconv('UTF-8', 'gbk', $params['charge_user']); // 交通工具
        $params['travel_place'] = iconv('UTF-8', 'gbk', $params['travel_place']); // 交通工具
        $params['reason'] = iconv('UTF-8', 'gbk', $params['reason']); // 交通工具
        $params['contacter'] = iconv('UTF-8', 'gbk', $params['contacter']); // 交通工具
        $params['target'] = iconv('UTF-8', 'gbk', $params['target']); // 交通工具
//        $params['creator'] = iconv('UTF-8', 'gbk', $params['creator']); // 交通工具
//        $params['creator_id'] = $params['creator_id'];
        $params['create_time'] = $time;
        $traffic_self_list = $params['number2'];
        foreach ($traffic_self_list as $ky => $val) {
            $json[$ky]['traffic_self'] = $params['roundtrip'][$ky]; // 部门预定 TODO: 组装json
            $json[$ky]['peoplecount'] = $params['peoplecount'][$ky]; // 部门预定 TODO: 组装json
            $json[$ky]['arrivetotime'] = $params['arrivetotime'][$ky]; // 部门预定 TODO: 组装json
            $json[$ky]['flight'] = $params['flight'][$ky]; // 部门预定 TODO: 组装json
            $json[$ky]['context'] = $params['context'][$ky]; // 部门预定 TODO: 组装json
        }
        $traffic_business_receive_list = $params['number3'];
        foreach ($traffic_business_receive_list as $k => $v) {
            $json1[$k]['StartTime'] = $params['StartTime'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['onwer'] = $params['onwer'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['customer'] = $params['customer'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['detailreason'] = $params['detailreason'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['receive'] = $params['receive'][$k]; // 业务招待 TODO: 组装json
            $json1[$k]['businessreason'] = $params['businessreason'][$k]; // 业务招待 TODO: 组装json
            $params['business_money'] = array_sum($params['receive']); // 业务费预算小计
        }
        $params['traffic_self'] = json_encode($json); // 部门预定 TODO: 组装json
        $params['business_receive'] = json_encode($json1); //业务招待 TODO: 组装json
        $json2['0']['project_name'] = "交通费"; // 费用预算 --- 项目名
        $json2['0']['trafficdetail'] = $params['trafficdetail']; // 费用预算 --- 详细内容
        $json2['0']['traffictotal'] = $params['traffictotal']; // 费用预算 --- 决算金额
        $json2['0']['trafficreason'] = $params['trafficreason']; // 费用预算 --- 备注
        $json2['1']['project_name'] = "住宿费"; // 费用预算 --- 项目名
        $json2['1']['staydetail'] = $params['staydetail']; // 费用预算 --- 详细内容
        $json2['1']['staytotal'] = $params['staytotal']; // 费用预算 --- 决算金额
        $json2['1']['stayreason'] = $params['stayreason']; // 费用预算 --- 备注
        $json2['2']['project_name'] = "餐费"; // 费用预算 --- 项目名
        $json2['2']['foodetail'] = $params['foodetail']; // 费用预算 --- 详细内容
        $json2['2']['foototal'] = $params['foototal']; // 费用预算 --- 决算金额
        $json2['2']['fooreason'] = $params['fooreason']; // 费用预算 --- 备注
        $json2['3']['project_name'] = "招待费"; // 费用预算 --- 项目名
        $json2['3']['meetdetail'] = $params['meetdetail']; // 费用预算 --- 详细内容
        $json2['3']['meettotal'] = $params['meettotal']; // 费用预算 --- 决算金额
        $json2['3']['meetreason'] = $params['meetreason']; // 费用预算 --- 备注
        $json2['4']['project_name'] = "出差补贴"; // 费用预算 --- 项目名
        $json2['4']['traveldetail'] = $params['traveldetail']; // 费用预算 --- 详细内容
        $json2['4']['traveltotal'] = $params['traveltotal']; // 费用预算 --- 决算金额
        $json2['4']['travelreason'] = $params['travelreason']; // 费用预算 --- 备注
        $json2['5']['project_name'] = "其他费用"; // 费用预算 --- 项目名
        $json2['5']['otherdetail'] = $params['otherdetail']; // 费用预算 --- 详细内容
        $json2['5']['othertotal'] = $params['othertotal']; // 费用预算 --- 决算金额
        $json2['5']['otherreason'] = $params['otherreason']; // 费用预算 --- 备注
        $params['budget_item'] = json_encode($json2); // 费用预算 TODO: 组装json
        $params['budget_money'] = $params['traffictotal'] + $params['staytotal'] + $params['foototal'] + $params['meettotal'] + $params['traveltotal'] + $params['othertotal']; // 费用预算合计
        //$travel_apply['cost_bear'] = $_POST['cost_bear']; // 费用承担
        $params['depart_opinion'] = iconv('UTF-8', 'gbk', $params['depart_opinion']); // 部门意见
        $params['politic_opinion'] = iconv('UTF-8', 'gbk', $params['politic_opinion']); // 行政意见
        $params['finance_opinion'] = iconv('UTF-8', 'gbk', $params['finance_opinion']); // 财务意见
        $params['finance_total'] = $params['finance_total']; // 财务建议支出金额
        $params['manager_opinion'] = iconv('UTF-8', 'gbk', $params['manager_opinion']); // 总经理意见
        $params['remark'] = iconv('UTF-8', 'gbk', $params['remark']); // 申请单备注
        $params['travel_status'] = "publish";
        // 保存travel apply 数据表
        unset($params['hdnid']);unset($params["travel_status"]);unset($params['number']);unset($params['number1']);unset($params['number2']);unset($params['number3']);unset($params['Time']);unset($params['trip_plan']);unset($params['live_place']);unset($params['customer_name']);unset($params['item_id']);unset($params['roundtrip']);unset($params['peoplecount']);unset($params['arrivetotime']);unset($params['flight']);unset($params['context']);unset($params['StartTime']);unset($params['onwer']);unset($params['customer']);unset($params['detailreason']);unset($params['receive']);unset($params['businessreason']);unset($params['alltotal']);unset($params['trafficdetail']);unset($params['traffictotal']);unset($params['trafficreason']);unset($params['staydetail']);unset($params['staytotal']);unset($params['stayreason']);unset($params['foodetail']);unset($params['foototal']);unset($params['fooreason']);unset($params['meetdetail']);unset($params['meettotal']);unset($params['meetreason']);unset($params['traveldetail']);unset($params['traveltotal']);unset($params['travelreason']);unset($params['otherdetail']);unset($params['othertotal']);unset($params['otherreason']);unset($params['cost_bearing']);
        Db::name('travel_apply')->where('id', '=', $id)->update($params);
        return true;
    }

    /*
     * 是否显示审批框 -- 审批权限
     */
    public function ShowInput($id, $uid){
        $result = Db::name('travel_apply')->where('id', '=', $id)->select();
        if($result['0']['creator_id'] == $uid){
            return array('status' => '1101', 'msg' => '当前登录的用户为申请人 - 部分input内容不可填写');
        }else{
            $username = GetUserNameByUid($uid); // 获取当前登录人的姓名 用于后续查询匹配权限
            $approve = Db::name('approval_proces')->where('step3', '=', $username)->select(); // 多重判断 1 是否为 人事
            if(!empty($approve)){
                return array('status' => '1102', 'msg' => '当前登录的用户为行政部审批人 - 允许操作行政审批input内容');
            }
            $approve = Db::name('approval_proces')->where('step4', '=', $username)->select(); // 多重判断 2 是否为 财务
            if(!empty($approve)){
                return array('status' => '1103', 'msg' => '当前登录的用户为财务部审批人 - 允许操作财务审批input内容');
            }
            if($uid == '13'){
                return array('status' => '1104', 'msg' => '当前登录的用户为总经理 - 允许操作总经理审批input内容');
            }
        }
    }

    /*
     * 是否显示审批框 -- 审批权限
     */
    public function ShowButton($id, $uid){
        $result = Db::name('travel_apply')->where('id', '=', $id)->select();
        if($result['0']['creator_id'] == $uid){
            return array('status' => '1101', 'msg' => '当前登录的用户为申请人 - 部分input内容不可填写');
        }else{
            $username = GetUserNameByUid($uid); // 获取当前登录人的姓名 用于后续查询匹配权限
            $approve = Db::name('approval_proces')->where('step3', '=', $username)->select(); // 多重判断 1 是否为 人事
            if(!empty($approve)){
                return array('status' => '1102', 'msg' => '当前登录的用户为行政部审批人 - 允许操作行政审批input内容');
            }
            $approvel = Db::name('approval_proces')->where('step4', '=', $username)->select(); // 多重判断 2 是否为 财务
            if(!empty($approvel)){
                return array('status' => '1103', 'msg' => '当前登录的用户为财务部审批人 - 允许操作财务审批input内容');
            }
            if($uid == '13'){
                return array('status' => '1104', 'msg' => '当前登录的用户为总经理 - 允许操作总经理审批input内容');
            }
        }
    }

    /*
     * 根据姓名获取审批流流程结果
     */
    public function GetApproveListByName($name){
        $result = Db::name('new_user')->where('user_name', '=', $name)->select();
        return $result['0']['uid'];
    }
    /*
     * 审批
     */
    public function ApproveTo($data, $uid){
        $id = $data['id']; // 审批单id
        $datainfo = Db::name('travel_apply')->where('id','=', $id)->select();
        $uname = GetUserNameByUid($uid);
        $uname = str_replace(',', '', $uname);
        $creater_user = Db::name('travel_apply')->where('id', '=', $data['id'])->select();
        $department = Db::name('new_user')->where('uid','=',$creater_user['0']['creator_id'])->select();
        # 根据当前登录的用户 来逐一判定是否为 部门经理 行政 财务 总经理
        $approve = Db::name('approval_proces')->where('department_name','like',"%".$department['0']['department_name'])->select();
        $appid_step = Db::name('new_user')->where('user_name','=', $approve['0']['step1'])->select();
        $appid_step_one = Db::name('new_user')->where('user_name','=', $approve['0']['step3'])->select();
        $appid_step_tow = Db::name('new_user')->where('user_name','=', $approve['0']['step4'])->select();
        if($uname == $approve['0']['step1']){
            $data['to_user_step'] = $uname;
            $data['to_status_step'] = '1';
            $data['to_uid_step'] = $uid;
            $data['to_user_step1'] = $approve['0']['step3'];
            $data['to_status_step1'] = '3';
            $data['to_uid_step1'] = $appid_step_one['0']['uid'];
            $output = CustomergetUserIdByUid($this->GetApproveListByName($approve['0']['step3'])).",".'您有一条待审批的出差单明细'.","."erp5/public/tripapi/travel/index_pt?nids=".$id;
        }else{
            if($uname == $approve['0']['step3']){
                if($datainfo['0']['financial_advance'] == '1'){
                    $data['to_user_step1'] = $approve['0']['step4'];
                    $data['to_status_step1'] = '4';
                    $data['to_uid_step1'] = $appid_step_tow['0']['uid'];
                    $output = CustomergetUserIdByUid($this->GetApproveListByName($approve['0']['step4'])).",".'您有一条待审批的出差单明细'.","."erp5/public/tripapi/travel/index_pt?nids=".$id;
                }else{
                    $data['to_user_step1'] = $approve['0']['step2'];
                    $data['to_status_step1'] = '2';
                    $data['to_uid_step1'] = $this->GetApproveListByName($approve['0']['step2']);
                    $output = CustomergetUserIdByUid($this->GetApproveListByName($approve['0']['step2'])).",".'您有一条待审批的出差单明细'.","."erp5/public/tripapi/travel/index_pt?nids=".$id;
                }
            }elseif($uname == $approve['0']['step4']){
                $data['to_user_step1'] = $approve['0']['step2'];
                $data['to_status_step1'] = '2';
                $data['to_uid_step1'] = $this->GetApproveListByName($approve['0']['step2']);
                $output = CustomergetUserIdByUid($this->GetApproveListByName($approve['0']['step2'])).",".'您有一条待审批的出差单明细'.","."erp5/public/tripapi/travel/index_pt?nids=".$id;
            }elseif($uname == $approve['0']['step2']){
                $data['to_user_step1'] = $approve['0']['step3'];
                $data['to_status_step1'] = '5';
                $data['to_uid_step1'] = $appid_step_one['0']['uid'];
                $output = CustomergetUserIdByUid($this->GetApproveListByName($approve['0']['step3'])).",".'您有一条已归档出差单明细'.","."erp5/public/tripapi/travel/index_pt?nids=".$id;
            }else{
                $data['to_user_step'] = $approve['0']['step1'];
                $data['to_status_step'] = '0';
                $data['to_uid_step'] = $appid_step['0']['uid'];
                $output = CustomergetUserIdByUid($this->GetApproveListByName($approve['0']['step1'])).",".'您有一条待审批的出差单明细'.","."erp5/public/tripapi/travel/index_pt?nids=".$id;
            }
        }
        Db::name('travel_apply')->where('id','=', $data['id'])->update($data);
        return array('status' => '200', 'params' => $output);
    }

}
?>